/**
 * Author : duchuynh
 * Nov 10, 2011
 */
package wte.rnd.buildingontology.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class StringProcessing {
		
	public StringProcessing(){
		
	}
	public static ArrayList<String> detectEntity(String inputString){		
		ArrayList<String> listEnableEntity = new ArrayList<String>();
		String[] tokens = inputString.split(" ");
		for (int i = 0; i < tokens.length; i++) {
			if(!tokens[i].equals("")){
				//System.out.println(tokens[i]);
				if(hasFirstCharacterIsUpperCharacter(tokens[i])){
					StringBuilder str = new StringBuilder();
					str.append(tokens[i]);				
					for (int j = (i + 1); j < (i + 6); j++) {
						if(!tokens[j].equals("")){
							//System.out.println(tokens[j]);
							if(hasFirstCharacterIsUpperCharacter(tokens[j])){
								str.append(" ");
								str.append(tokens[j]);
							}else j = i + 6;
						}
					}
				listEnableEntity.add(str.toString());
				}
			}
		}	
		return listEnableEntity;		
	}
	
	public static ArrayList<String> findEntityFormURL(URL url) throws IOException{		
		URLConnection yc = url.openConnection();
		BufferedReader br = new BufferedReader(new InputStreamReader(yc.getInputStream()));
		String line;						
		StringBuilder string = new StringBuilder();
		while(null != (line = br.readLine())){
			string.append(" ");
			string.append(line.replaceAll("\\<[^>]*>","").replaceAll("\\t", ""));
		}
		ArrayList<String> listEnableEntity = StringProcessing.detectEntity(string.toString());
		return listEnableEntity;			
	}
	
	public static boolean hasFirstCharacterIsUpperCharacter(String str){
		char[] keys = str.toCharArray();		
		if(Character.isLetter(keys[0]) && Character.isUpperCase(keys[0])) {
			return true;
		} else return false;
	}	
}

